#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
struct streamclient{
    int a;
    char* s;
    void* (*cb)(int a, char* s);
};


int streamclient_setopt_valist(struct streamclient* sc, int tag, va_list param) 
{
    int ret = 0;

    int a;
    const char* s;
    void* (*cb)(int a, char* s);
    switch(tag) {
        case 'a':
        a = va_arg(param, int);
        sc->a = a;
        break;

        case 's':
        s = va_arg(param, const char*);
        sc->s = strdup(s);
        break;

        case 'c':
        a = va_arg(param, int);
        s = va_arg(param, const char*);
        cb = va_arg(param, void* (*)(int a, char* s));
        sc->a = a;
        sc->s = strdup(s);
        sc->cb = cb;
        break;

        default:
        ret = -1;
        break;
    }

    return ret;
}


int streamclient_setopt(struct streamclient* sc, int tag, ...)
{
    int ret = 0;

    va_list arg;

    va_start(arg, tag);
    streamclient_setopt_valist(sc, tag, arg);
    va_end(arg);

    return ret;
}


void* snp(int a, char* s)
{
    sprintf(s, "%d", a);
    return s;
}


int main()
{
    struct streamclient* sc = malloc(sizeof(*sc));
    memset(sc, 0, sizeof(*sc));
    printf("a=%d, s=%s, cb=%p\n", sc->a, sc->s, sc->cb);

    streamclient_setopt(sc, 'a', 100);
    printf("a=%d, s=%s, cb=%p\n", sc->a, sc->s, sc->cb);
    memset(sc, 0, sizeof(*sc));

    streamclient_setopt(sc, 's', "abc");
    printf("a=%d, s=%s, cb=%p\n", sc->a, sc->s, sc->cb);
    memset(sc, 0, sizeof(*sc));

    streamclient_setopt(sc, 'c', 1, "sss", snp);
    printf("a=%d, s=%s, cb=%p\n", sc->a, sc->s, sc->cb);
    memset(sc, 0, sizeof(*sc));
    
    return 0;
}
